import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
df = pd.read_csv("Estonian citizens abroad.csv",
header=0)
df.head()
| Nimi | Name | iso_alpha | # of Estonian citizens | Continent | Sub-region | Capital | Distance between Capital and Tallinn (km) | Population (2020) | GDP PPP per capita, 2020 (INTUSD) | Formerly part of the USSR? | Sovereignty | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Soome | Finland | FIN | 54158 | Europe | Northern Europe | Helsinki | 82.8 | 5540718 | 50506.28590 | No | Sovereign |
| 1 | Venemaa | Russia | RUS | 19197 | Europe | Eastern Europe | Moscow | 867.4 | 145934460 | 29812.21025 | Yes | Sovereign |
| 2 | Suurbritannia | United Kingdom | GBR | 9531 | Europe | Northern Europe | London | 1783.3 | 67886004 | 46482.86224 | No | Sovereign |
| 3 | Saksamaa | Germany | DEU | 5592 | Europe | Western Europe | Berlin | 1042.2 | 83783945 | 54792.06448 | No | Sovereign |
| 4 | Rootsi | Sweden | SWE | 5044 | Europe | Northern Europe | Stockholm | 378.1 | 10099270 | 55037.72390 | No | Sovereign |
target = df["# of Estonian citizens"]
target_label = "# of Estonian citizens"
name = df["Name"]
name_label = "Name"
distance = df["Distance between Capital and Tallinn (km)"]
df["Name and number of citizens"] = name.map(str) + '\n' + target.map(str)
name_target = df["Name and number of citizens"]
name_target_label = "Name and number of citizens"
# normalize distances
distance_norm = (distance-distance.min())/ (distance.max() - distance.min())
fig = px.scatter_geo(df, locations="iso_alpha",
hover_name="Name", size = df[target_label]**0.4,
projection="natural earth")
fig.update_traces(customdata=df[name_target_label])
fig.update_traces(hovertemplate='%{customdata}<extra></extra>')
fig.show()
fig = px.treemap(df[0:11], path=[name_target_label],values=target_label)
fig.show()
C:\Users\alexa\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\express\_core.py:1637: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
fig = px.treemap(df[11:31], path=[name_target_label],values=target_label)
fig.show()
C:\Users\alexa\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\express\_core.py:1637: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
fig = px.treemap(df[31:61], path=[name_target_label],values=target_label)
fig.show()
C:\Users\alexa\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\express\_core.py:1637: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
fig = px.treemap(df[61:], path=[name_target_label],values=target_label)
fig.show()
C:\Users\alexa\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\express\_core.py:1637: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
plt.figure(figsize=(16,6))
plt.scatter(distance_norm[0:15],target[0:15],s=100,color="red")
plt.xlabel("Distance between country capital and Tallinn\n(normalized)")
plt.ylabel("Number of Estonian citizens\n(normalized)")
plt.title("Number of Estonian citizens living in a given country\nvs\n Distance between country capital and Tallinn",fontsize=15)
for i, label in enumerate(name[0:15]):
plt.text(distance_norm[0:15][i], target[0:15][i],label)
plt.show()